﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VocabManager.ViewModel;
using Microsoft.Phone.Shell;

namespace VocabManager.View
{
    public partial class ItemView : UserControlBase
    {
        ItemViewModel _viewModel;


        public ItemView()
        {
            InitializeComponent();
        }
        public void SetViewModel(ItemViewModel viewModel)
        {
            _viewModel = viewModel;
        }
        protected override void OnLoad()
        {
            base.OnLoad();
            SetViewModel(this.DataContext as ItemViewModel);

            _viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_viewModel_PropertyChanged);
            InitChildBasedViewModelMode(); ;
        }

        void _viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsInEditMode")
            {
                InitChildBasedViewModelMode();
            }
        }

        private void InitChildBasedViewModelMode()
        {
            if (_viewModel.IsInEditMode)
                LoadNewChild(new ItemViewModeEdit());
            else
                LoadNewChild(new ItemViewModeView());
        }

        private void LoadNewChild(UserControlBase newView)
        {
            this.LayoutRoot.Children.Clear();
            this.LayoutRoot.Children.Add(newView);
        }
        protected override void OnUnload()
        {
            base.OnUnload();
            _viewModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_viewModel_PropertyChanged);

        }
    }
}
